
var util = require('../../utils/util.js');
var app = getApp();
var utilMd5 = require('../../utils/md5.js');
Page({

  data: {

    orgInfo: '',
  },


  onLoad: function (options) {
    console.log("传入的页面参数：", options);

    var that = this;

    wx.setNavigationBarTitle({
      title: '微信授权登录',
    });

    //获取小程序信息
    app.GetUserInfo(util.orgid, function (res) {
      console.log("小程序信息：", res);
      that.setData({
        orgInfo: res
      })
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });

  },


  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  onGotUserInfo: function (res) {
    console.log(res);
    console.log(res.detail.errMsg)
    console.log(res.detail.userInfo)
    console.log(res.detail.rawData)

    if(res.detail.userInfo){
      this.getLoginUserInfo(res.detail);
    } 
  },


  getLoginUserInfo: function (res) {
    //当前登录用户信息
    wx.showLoading({
      title: 'loading...',
      mask: true
    })
    var that = this
    var inviteuid = wx.getStorageSync(util.inviteKey) ? wx.getStorageSync(util.inviteKey) : '';
    wx.login({
      success: function (e) {
        console.log("wx.login success");
        var js_code = e.code;
        console.log("js_code:" + js_code);
        console.log("用户信息:", res);
        //会新建用户的 登录请求地址
        var loginUrl = util.api_host + '/index.php?app=w3g&mod=Public&act=loginWechatSmallCon&binduid=' + util.orgid;
        var params = {
          "code": js_code,
          "encryptedData": res.encryptedData,
          "iv": res.iv,
          "inviteuid": inviteuid
        };
        console.log("请求地址：" + loginUrl, params);
        wx.request({
          url: loginUrl,
          data: params,
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          success: function (response) {
            console.log("授权并登录小程序成功：", response);
            if (response.data.status == 1) {
              //登录成功，存储信息                    
              wx.setStorageSync('userInfo', response.data.data);
              //存储获取登录用户信息日期
              let date = new Date();
              let day = date.getDate();
              wx.setStorageSync('getuserInfoDay', day);
            } else {
              wx.showToast({
                title: response.data.message,
                icon: 'error',
                duration: 2000
              })
            }
            that.navigateBack();
          },
          fail: function (e) {

          }
        })

      },
      fail: function (e) {
        that.createOrRegetLoginUserInfo = false;
        console.log("wx.login fail:", e);
        if (goAuth) {
          wx.showModal({
            title: "温馨提示",
            content: "微信授权登录失败，请稍后重试",
            showCancel: false
          })
        }
      }
    });

  },


  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';

    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      backgroundColor: bg_color
    })
  },

  navigateBack: function () {
    wx.navigateBack({
      delta: 1
    })
  },

})